'use strict';
app.controller('tableCtrl', ['$scope','$state','toaster','$modal', '$log','utilService','tableService', function($scope,$state,toaster,$modal,$log,utilService,tableService) {

    //查询打印机列表
    $scope.findPageAll = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'table/number'
        }).then(function (result) {
        });
    }

    $scope.findPageAll();

    //打开添加窗口
    $scope.openAddPoup=function(){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/table/table_add.html',
            controller: 'tableAddCtrl',
            size: 'md',
            scope:$scope,
            backdrop:'static'
        });

        modalInstance.result.then(function (result) {
            $scope.findPageAll();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }

    //生成号码
    $scope.openRegenerate = function(id, name){
        var param = {
            id:id,
            name:name
        };

        openModal("重新生成二维码,之前的"+name+"二维码将会废弃,是否确定要重新生成");

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"重新生成二维码",
                controller:"regenerateCtrl",
                message:msg,
                data:param
            });
        }
    }

    $scope.openDelete = function(id, name){
        var param = {
            id:id,
            name:name
        };

        openModal("你是否要删除【" + name + "】桌号？");

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除桌号",
                controller:"deleteCtrl",
                message:msg,
                data:param
            });
        }
    }


    //批量选择
    $scope.selected = [];

    $scope.isChecked = function (item) {
        return $scope.selected.indexOf(item) >= 0;
    };

    $scope.updateSelection = function ($event, item) {

        var checkbox = $event.target;
        var checked = checkbox.checked;

        if (checked) {
            $scope.selected.push(item);
        } else {
            var idx = $scope.selected.indexOf(item);
            $scope.selected.splice(idx, 1);
        }
    };

    $scope.isCheckedAll = function () {

        if ($scope.list == null) {
            return;
        }

        return $scope.selected.length == $scope.list.length;
    }

    $scope.checkAll = function ($event) {
        var checkbox = $event.target;
        var checked = checkbox.checked;

        for (var i = 0; i < $scope.list.length; i++) {
            if (checked) {
                $scope.selected.push($scope.list[i]);
            } else {
                $scope.selected.splice(0, $scope.selected.length);
            }
        }
    }

    $scope.initSelect = function () {
        $scope.selected = [];
    }

    //下载
    $scope.download = function (item) {
        var filePaths=new Array();
        var obj=new Object();
        obj.name=item.code;
        obj.fileName=item.qrCode;
        filePaths.push(obj);
        pushImg(filePaths);

        tableService.download($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $modalInstance.close("success");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    //批量下载
    $scope.batchDownload=function(){
        if($scope.selected.length<=0){
            alert("请选择要下载的点餐码");
            return;
        }


        var filePaths=new Array();
        for(var i=0;i<$scope.selected.length;i++){
            var obj=new Object();
            obj.name=$scope.selected[i].code;
            obj.fileName=$scope.selected[i].qrCode;
            filePaths.push(obj);
        }
        pushImg(filePaths);

        tableService.download($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $modalInstance.close("success");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })

    }

    //
    var pushImg=function(filePaths){
        $scope.data={zipName:"点餐码"};
        $scope.data.filePaths=new Array();
        $scope.data.filePaths=JSON.stringify(filePaths);
    }


}]);

app.controller('tableAddCtrl', ['$scope','$modalInstance','$state','toaster','tableService', function($scope, $modalInstance,$state,toaster,tableService) {

    $scope.tableNumberAddVO={};
    $scope.tableNumberBatchAddVO={};


    $scope.addState=false;
    $scope.batchAddState=false;

    //查询品牌信息
    $scope.add = function () {
        if($scope.addState){
            return;
        }

        $scope.addState=true;

        tableService.add($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.addState=false;
                    $modalInstance.close("success");
                    break;
                case 639:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }


    $scope.batchAdd = function () {
        if($scope.batchAddState){
            return;
        }

        $scope.batchAddState=true

        tableService.batchAdd($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.batchAddState=false;
                    $modalInstance.close("success");
                    break;
                case 639:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    //查询所有
    $scope.getAll = function () {
        tableService.getAll($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.tableTypes=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.getAll();


    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };

}]);

app.controller('regenerateCtrl', ['$scope', '$modalInstance','$state','tableService','toaster','modalData',
    function($scope, $modalInstance,$state,tableService,toaster,modalData) {
        $scope.modal = modalData;

        $scope.id=$scope.modal.data.id;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            tableService.regenerate($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.isDelModal=true;
                        $scope.findPageAll();
                        break;
                    case 615:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }


        //查询所有
        $scope.getAll = function () {
            tableService.getAll($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        console.log(result.data);
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            })
        }

        $scope.getAll();

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


app.controller('deleteCtrl', ['$scope', '$modalInstance','$state','tableService','toaster','modalData',
    function($scope, $modalInstance,$state,tableService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.id=$scope.modal.data.id;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            tableService.updateToDelete($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.isDelModal=true;
                        $scope.findPageAll();
                        break;
                    case 615:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);



